package com.blb.mvc.controller;

import com.blb.mvc.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("user")
public class UserController {

    @PostMapping("/login")
    public String login(User user, Model model, HttpSession session){
//        String username = req.getParameter("username");
//        String password = req.getParameter("password");
//        System.out.println("username:"+username);
//        System.out.println("password:"+pwd);
        System.out.println(user);
        if("admin".equals(user.getUsername()) && "123".equals(user.getPassword())){
            //保存数据
            //model.addAttribute("user",user);
            session.setAttribute("user",user);
            //重定向
            return "redirect:/pages/index";
        }
        model.addAttribute("msg","账号或密码错误");
        //请求转发
        return "forward:/pages/login";
    }
}
